package com.sysd.entity;

import com.sysd.exception.AbstractException;
import lombok.Data;

import java.io.Serializable;

/**
 * @description:
 * @author: bsh
 * @date: 2025/5/30 10:45
 */
@Data
public class Result<T>implements Serializable {
    private static final long serialVersionUID = -8069552323735340766L;
    private int code;
    private String msg;
    private T data;

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(T data) {
        this.code = 0;
        this.msg = "成功";
        this.data = data;
    }

    public Result(CodeMsg codeMsg) {
        this.code = codeMsg.getCode();
        this.msg = codeMsg.getMsg();
        this.data = null;
    }

    public Result(AbstractException exp) {
        this.code = exp.getCode();
        this.msg = exp.getMsg();
        this.data = null;
    }

    /**
     * 成功返回无结果
     *
     * @return res
     */
    public static Result<String> success() {
        return new Result<>("");
    }

    /**
     * 成功返回有结果
     *
     * @param data 需要返回的数据
     * @param <T>  泛型
     * @return res
     */
    public static <T> Result<T> success(T data) {
        return new Result<>(data);
    }

    /**
     * 错误返回示例
     *
     * @param exp 异常对象
     * @param <T> 泛型
     * @return res
     */
    public static <T> Result<T> error(AbstractException exp) {
        return new Result<>(exp);
    }

    /**
     * 错误返回
     * @param codeMsg 状态码枚举类
     * @return res
     * @param <T> 泛型
     */
    public static <T> Result<T> error(CodeMsg codeMsg) {
        return new Result<>(codeMsg);
    }

    /**
     * 错误返回
     * @param codeMsg 状态码枚举类
     * @return res
     * @param <T> 泛型
     */
    public static <T> Result<T> error(CodeMsg codeMsg,String msg) {
        return new Result<>(codeMsg.getCode(),msg,null);
    }
}
